﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Sina.Altman.Weibo.Tools;
using System.IO;
using Sina.Altman.Weibo.SDK.Objects;

namespace Sina.Altman.Weibo.SDK.HttpService
{
    /// <summary>
    /// 该接口暂不能用
    /// </summary>
    public class RemindRequest
    {
        static string token = Enviroment.Token;
        /// <summary>
        /// 获取公共信息流，返回值可能为null注意检查
        /// 获取操作为异步操作
        /// </summary>
        /// <param name="parameters">可加参数</param>
        /// <returns></returns>
        public static void GetRequest(RemindType type, RemindRequestData data, Action<object> callback)
        {
            var request = (HttpWebRequest)WebRequest.Create(new Uri(GetRequestUri(type, data), UriKind.Absolute));
            request.Method = "GET";
            var inner = request.BeginGetResponse(ResponseCallback, new RemindUserState() { Callback = callback, Request = request, RequestData = data });

        }

        /// <summary>
        /// 回调 
        /// </summary>
        /// <param name="result"></param>
        private static void ResponseCallback(IAsyncResult result)
        {
            var userState = result.AsyncState as AccountUserState;

            try
            {
                var request = userState.Request as HttpWebRequest;
                var respone = (HttpWebResponse)request.EndGetResponse(result);

                var stream = respone.GetResponseStream();
                using (StreamReader reader = new StreamReader(stream))
                {
                    string jsonRes = reader.ReadToEnd();
                    if (!string.IsNullOrEmpty(jsonRes))
                    {
                        Remind uid = new Remind(jsonRes);
                        if (null != userState.Callback)
                        {
                            userState.Callback(uid);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (null != userState.Callback)
                    userState.Callback(e);
            }
        }

        /// <summary>
        /// 获取请求地址
        /// </summary>
        /// <param name="queryType"></param>
        /// <returns></returns>
        private static string GetRequestUri(RemindType queryType, RemindRequestData data)
        {
            if (string.IsNullOrEmpty(token))
                token = ISHelper.LoadSetting<string>(Constants.User_Access_Token_SR, string.Empty);
            string fullUri = string.Empty;

            switch (queryType)
            {
                case RemindType.Unread:
                    fullUri = Constants.UNREAD_COUNT + "?access_token=" + token;
                    break;
                default:
                    break;
            }

            return fullUri;
        }
    }

    public class RemindUserState:UserState
    {
        public RemindRequestData RequestData { get; set; }
    }

    public enum RemindType
    {
        Unread,
    }

    public class RemindRequestData:RequestData
    {
    }
}
